﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryAddress : ADONetRepositoryBase<Address>
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Address entity)
        {
            var cmd = new SqlCommand("AddressInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("PostIndex", entity.PostIndex));
            cmd.Parameters.Add(new SqlParameter("Country", entity.Country));
            cmd.Parameters.Add(new SqlParameter("City", entity.City));
            cmd.Parameters.Add(new SqlParameter("Street", entity.Street));
            cmd.Parameters.Add(new SqlParameter("House", entity.House));
            cmd.Parameters.Add(new SqlParameter("Flat", entity.Flat));
            logger.Trace("SqlCommand для записи данных Адреса в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("AddressDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Адреса id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Address entity)
        {
            var cmd = new SqlCommand("AddressUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("PostIndex", entity.PostIndex));
            cmd.Parameters.Add(new SqlParameter("Country", entity.Country));
            cmd.Parameters.Add(new SqlParameter("City", entity.City));
            cmd.Parameters.Add(new SqlParameter("Street", entity.Street));
            cmd.Parameters.Add(new SqlParameter("House", entity.House));
            cmd.Parameters.Add(new SqlParameter("Flat", entity.Flat));
            logger.Trace("SqlCommand для обновления данных Адреса id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetAddress", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Адреса id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            throw new NotImplementedException();
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetAddressByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Адреса {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Address GetEntity(SqlDataReader sqlDataReader)
        {
            Address address = new Address();
            address.Id = (int)sqlDataReader["Id"];
            address.PostIndex = sqlDataReader["PostIndex"].ToString();
            address.Country = sqlDataReader["Country"].ToString();
            address.City = sqlDataReader["City"].ToString();
            address.Street = sqlDataReader["Street"].ToString();
            address.House = sqlDataReader["House"].ToString();
            address.Flat = sqlDataReader["Flat"].ToString();
            return address;
        }
    }
}
